<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CollectionController extends Controller
{
    /*
        执行店铺收藏
    */
    public function getInsert(Request $request)
    {
        //判断用户是否登录
        if(empty(session('id'))){
            return 2;
        }

        //接收店铺的id
        $market_id = $request->input('id');
        //接收用户的id
        $user_id = session('id');
        //定义空数组
        $data = [];
        $data['market_id'] = $market_id;
        $data['user_id'] = $user_id;

        //判断用户是否已经收藏
        $aa = DB::table('collection_shop')->where('user_id',$user_id)->where('market_id',$market_id)->first();
        if(!empty($aa)){
            return 4;
        }
        
        //执行添加
        $res = DB::table('collection_shop')->insert($data);
        //判断执行结果
        if($res){
            return 1;
        }else{
            return 3;
        }
    }

    /*
        执行收藏宝贝
    */
    public function getInsertgoods(Request $request)
    {
        //判断用户是否登录
        if(empty(session('id'))){
            return 2;
        }

        //接收商品id
        $goods_id = $request->input('id');
        //接收用户id
        $user_id = session('id');
        //定义空数组
        $data = [];
        $data['goods_id'] = $goods_id;
        $data['user_id'] = $user_id;

        //判断用户是否已经收藏
        $aa = DB::table('collection_goods')->where('user_id',$user_id)->where('goods_id',$goods_id)->first();
        if(!empty($aa)){
            return 4;
        }

        //执行添加
        $res = DB::table('collection_goods')->insert($data);
        //判断执行结果
        if($res){
            return 1;
        }else{
            return 3;
        }

    }

    /*
        显示收藏页面
    */
    public function getIndex($status)
    {
        //判断用户是否登录
        if(!session('id')){
            return redirect('/login')->with('error','亲,你还没有登录哦!');
        }
        //查询用户头像
        $pic = DB::table('user')->where('id',session('id'))->value('pic');

        //判断用户是需要 查询收藏店铺 还是 查询收藏商品
        if($status == 1){
            $menu = HomeListController::menu(0);
            //查询收藏商品
            $collection = DB::table('collection_goods')
                        ->join('goods','collection_goods.goods_id','=','goods.id')
                        ->select('collection_goods.*','goods.goods_pic','goods.goods_name','goods.goods_price','goods.goods_stock')
                        ->where('collection_goods.user_id',session('id'))
                        ->paginate(6);
            //查询当前用户是否有收藏商品
            $num = count(DB::table('collection_goods')->where('user_id',session('id'))->get());
            //解析商品模板
            return view('home.collection.goods',['collection'=>$collection,'pic'=>$pic,'num'=>$num,'menu'=>$menu]);
        }else if($status == 2){
            $menu = HomeListController::menu(0);
            //查询收藏店铺
            $collection = DB::table('collection_shop')
                        ->join('market','collection_shop.market_id','=','market.id')
                        ->select('collection_shop.*','market.mar_name','market.mar_pic')
                        ->where('collection_shop.user_id',session('id'))
                        ->paginate(8);
            //查询当前用户是否有收藏店铺
            $num = count(DB::table('collection_shop')->where('user_id',session('id'))->get());
            //解析店铺模板
            return view('home.collection.favorite',['collection'=>$collection,'pic'=>$pic,'num'=>$num,'menu'=>$menu]);
        }else{
            abort(404);
        }
    }

    /*
        删除收藏店铺
    */
    public function getDeleteshop(Request $request)
    {
        //接收id
        $id = $request->input('id');
        //删除
        $res = DB::table('collection_shop')->where('id',$id)->delete();
        //判断
        if($res){
            return 1;
        }else{
            return 2;
        }
    }
}  
